package com.vint.leetcode;

import com.vint.common.TreeNode;

public class Leetcode543 {
    public int diameterOfBinaryTree(TreeNode root) {
        if(null==root || (null==root.left&&null==root.right)){
            return 0;
        }
        int tree = lenghtOfTree(root.left)+lenghtOfTree(root.right);
        int left = diameterOfBinaryTree(root.left);
        int right = diameterOfBinaryTree(root.right);
        return Math.max(Math.max(left,right),tree);
    }

    public int lenghtOfTree(TreeNode root){
        if(null==root){
            return 0;
        }
        if(null==root.left&&null==root.right){
            return 1;
        }
        if(lenghtOfTree(root.left) > lenghtOfTree(root.right)){
            return lenghtOfTree(root.left) + 1;
        }else{
            return lenghtOfTree(root.right) + 1;
        }
    }
    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(1);
        TreeNode root2 = new TreeNode(1);
        root2.right = new TreeNode(1);
        root2.left = new TreeNode(1);
        root2.right.right = new TreeNode(1);
        Leetcode543 leetcode543 = new Leetcode543();
        int ret1 = leetcode543.diameterOfBinaryTree(root1);
        int ret2 = leetcode543.diameterOfBinaryTree(root2);

        System.out.println("success!");
    }
}
